import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
var list=JSON.parse(localStorage.getItem('getlist'))||[]
export default new Vuex.Store({
  state: {
    // 存储购物车数据
    getlists:list,
  
  },
  getters:{
    //  计算价格
    jiage:function(state){
    var o={
       jg:0,
       sl:0
    } 
      state.getlists.forEach(v=>{
        if(v.cla){
         o.jg+=v.goods_price*v.num
         o.sl+=v.num
        }
      })
      return o
    }
  },
  // 
  mutations: {
    // 加入购物车
     jrgwc(state,item_index){
      
      let ind=state.getlists.findIndex(v=>v.goods_id==item_index.goods_id)
      if(ind===-1){
        console.log("不存在")
        item_index.num=1
        item_index.cla=true
        state.getlists.push(item_index)
      }else{
        console.log("存在")
        state.getlists[ind].num++
      }
      
      localStorage.setItem('getlist',JSON.stringify(state.getlists))
     
      console.log(state.getlists,123122)
     }, 
    //  选中商品
    // xzsp(state,index){
    //   console.log(index)
    //   state.getlists[index].cla=!state.getlists[index].cla
    //   localStorage.setItem('getlist',JSON.stringify(state.getlists))
  
    // }
  },
    actions: {
  },
    modules: {
  }
})
